<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Customizing the comments framework &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../index.html" />
    <link rel="up" title="Django’s comments framework" href="index.html" />
    <link rel="next" title="Comment form classes" href="forms.html" />
    <link rel="prev" title="Signals sent by the comments app" href="signals.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="signals.html" title="Signals sent by the comments app">previous</a>
     |
    <a href="../../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="forms.html" title="Comment form classes">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-comments-custom">
            
  <div class="section" id="s-customizing-the-comments-framework">
<span id="customizing-the-comments-framework"></span><h1>Customizing the comments framework<a class="headerlink" href="#customizing-the-comments-framework" title="Permalink to this headline">¶</a></h1>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p>Django&#8217;s comment framework has been deprecated and is no longer supported.
Most users will be better served with a custom solution, or a hosted
product like <a class="reference external" href="https://disqus.com/">Disqus</a>.</p>
<p class="last">The code formerly known as <tt class="docutils literal"><span class="pre">django.contrib.comments</span></tt> is <a class="reference external" href="https://github.com/django/django-contrib-comments">still available
in an external repository</a>.</p>
</div>
<p>If the built-in comment framework doesn&#8217;t quite fit your needs, you can extend
the comment app&#8217;s behavior to add custom data and logic. The comments framework
lets you extend the built-in comment model, the built-in comment form, and the
various comment views.</p>
<p>The <a class="reference internal" href="../../settings.html#std:setting-COMMENTS_APP"><tt class="xref std std-setting docutils literal"><span class="pre">COMMENTS_APP</span></tt></a> setting is where this customization begins. Set
<a class="reference internal" href="../../settings.html#std:setting-COMMENTS_APP"><tt class="xref std std-setting docutils literal"><span class="pre">COMMENTS_APP</span></tt></a> to the name of the app you&#8217;d like to use to provide
custom behavior. You&#8217;ll use the same syntax as you&#8217;d use for
<a class="reference internal" href="../../settings.html#std:setting-INSTALLED_APPS"><tt class="xref std std-setting docutils literal"><span class="pre">INSTALLED_APPS</span></tt></a>, and the app given must also be in the
<a class="reference internal" href="../../settings.html#std:setting-INSTALLED_APPS"><tt class="xref std std-setting docutils literal"><span class="pre">INSTALLED_APPS</span></tt></a> list.</p>
<p>For example, if you wanted to use an app named <tt class="docutils literal"><span class="pre">my_comment_app</span></tt>, your
settings file would contain:</p>
<div class="highlight-python"><div class="highlight"><pre>INSTALLED_APPS = [
    ...
    &#39;my_comment_app&#39;,
    ...
]

COMMENTS_APP = &#39;my_comment_app&#39;
</pre></div>
</div>
<p>The app named in <a class="reference internal" href="../../settings.html#std:setting-COMMENTS_APP"><tt class="xref std std-setting docutils literal"><span class="pre">COMMENTS_APP</span></tt></a> provides its custom behavior by
defining some module-level functions in the app&#8217;s <tt class="docutils literal"><span class="pre">__init__.py</span></tt>. The
<a class="reference internal" href="#custom-comment-app-api"><em>complete list of these functions</em></a> can be found
below, but first let&#8217;s look at a quick example.</p>
<div class="section" id="s-an-example-custom-comments-app">
<span id="an-example-custom-comments-app"></span><h2>An example custom comments app<a class="headerlink" href="#an-example-custom-comments-app" title="Permalink to this headline">¶</a></h2>
<p>One of the most common types of customization is modifying the set of fields
provided on the built-in comment model. For example, some sites that allow
comments want the commentator to provide a title for their comment; the built-in
comment model has no field for that title.</p>
<p>To make this kind of customization, we&#8217;ll need to do three things:</p>
<ol class="arabic simple">
<li>Create a custom comment <a class="reference internal" href="../../models/instances.html#django.db.models.Model" title="django.db.models.Model"><tt class="xref py py-class docutils literal"><span class="pre">Model</span></tt></a> that adds on the
&#8220;title&#8221; field.</li>
<li>Create a custom comment <a class="reference internal" href="../../forms/api.html#django.forms.Form" title="django.forms.Form"><tt class="xref py py-class docutils literal"><span class="pre">Form</span></tt></a> that also adds this
&#8220;title&#8221; field.</li>
<li>Inform Django of these objects by defining a few functions in a
custom <a class="reference internal" href="../../settings.html#std:setting-COMMENTS_APP"><tt class="xref std std-setting docutils literal"><span class="pre">COMMENTS_APP</span></tt></a>.</li>
</ol>
<p>So, carrying on the example above, we&#8217;re dealing with a typical app structure in
the <tt class="docutils literal"><span class="pre">my_comment_app</span></tt> directory:</p>
<div class="highlight-python"><div class="highlight"><pre>my_comment_app/
    __init__.py
    models.py
    forms.py
</pre></div>
</div>
<p>In the <tt class="docutils literal"><span class="pre">models.py</span></tt> we&#8217;ll define a <tt class="docutils literal"><span class="pre">CommentWithTitle</span></tt> model:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.contrib.comments.models</span> <span class="kn">import</span> <span class="n">Comment</span>

<span class="k">class</span> <span class="nc">CommentWithTitle</span><span class="p">(</span><span class="n">Comment</span><span class="p">):</span>
    <span class="n">title</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">300</span><span class="p">)</span>
</pre></div>
</div>
<p>Most custom comment models will subclass the
<a class="reference internal" href="models.html#django.contrib.comments.models.Comment" title="django.contrib.comments.models.Comment"><tt class="xref py py-class docutils literal"><span class="pre">Comment</span></tt></a> model. However,
if you want to substantially remove or change the fields available in the
<a class="reference internal" href="models.html#django.contrib.comments.models.Comment" title="django.contrib.comments.models.Comment"><tt class="xref py py-class docutils literal"><span class="pre">Comment</span></tt></a> model, but don&#8217;t want to
rewrite the templates, you could try subclassing from
<tt class="docutils literal"><span class="pre">BaseCommentAbstractModel</span></tt>.</p>
<p>Next, we&#8217;ll define a custom comment form in <tt class="docutils literal"><span class="pre">forms.py</span></tt>. This is a little more
tricky: we have to both create a form and override
<tt class="docutils literal"><span class="pre">CommentForm.get_comment_model()</span></tt> and
<tt class="docutils literal"><span class="pre">CommentForm.get_comment_create_data()</span></tt> to return deal with our custom title
field:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
<span class="kn">from</span> <span class="nn">django.contrib.comments.forms</span> <span class="kn">import</span> <span class="n">CommentForm</span>
<span class="kn">from</span> <span class="nn">my_comment_app.models</span> <span class="kn">import</span> <span class="n">CommentWithTitle</span>

<span class="k">class</span> <span class="nc">CommentFormWithTitle</span><span class="p">(</span><span class="n">CommentForm</span><span class="p">):</span>
    <span class="n">title</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">300</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_comment_model</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># Use our custom comment model instead of the built-in one.</span>
        <span class="k">return</span> <span class="n">CommentWithTitle</span>

    <span class="k">def</span> <span class="nf">get_comment_create_data</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># Use the data of the superclass, and add in the title field</span>
        <span class="n">data</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">CommentFormWithTitle</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_comment_create_data</span><span class="p">()</span>
        <span class="n">data</span><span class="p">[</span><span class="s">&#39;title&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&#39;title&#39;</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">data</span>
</pre></div>
</div>
<p>Django provides a couple of &#8220;helper&#8221; classes to make writing certain types of
custom comment forms easier; see <a class="reference internal" href="forms.html#module-django.contrib.comments.forms" title="django.contrib.comments.forms: Forms for dealing with the built-in comment model."><tt class="xref py py-mod docutils literal"><span class="pre">django.contrib.comments.forms</span></tt></a> for
more.</p>
<p>Finally, we&#8217;ll define a couple of methods in <tt class="docutils literal"><span class="pre">my_comment_app/__init__.py</span></tt> to
point Django at these classes we&#8217;ve created:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">my_comment_app.models</span> <span class="kn">import</span> <span class="n">CommentWithTitle</span>
<span class="kn">from</span> <span class="nn">my_comment_app.forms</span> <span class="kn">import</span> <span class="n">CommentFormWithTitle</span>

<span class="k">def</span> <span class="nf">get_model</span><span class="p">():</span>
    <span class="k">return</span> <span class="n">CommentWithTitle</span>

<span class="k">def</span> <span class="nf">get_form</span><span class="p">():</span>
    <span class="k">return</span> <span class="n">CommentFormWithTitle</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p>Be careful not to create cyclic imports in your custom comments app.
If you feel your comment configuration isn&#8217;t being used as defined &#8211;
for example, if your comment moderation policy isn&#8217;t being applied &#8211;
you may have a cyclic import problem.</p>
<p class="last">If you are having unexplained problems with comments behavior, check
if your custom comments application imports (even indirectly)
any module that itself imports Django&#8217;s comments module.</p>
</div>
<p>The above process should take care of most common situations. For more
advanced usage, there are additional methods you can define. Those are
explained in the next section.</p>
</div>
<div class="section" id="s-custom-comment-app-api">
<span id="s-id3"></span><span id="custom-comment-app-api"></span><span id="id3"></span><h2>Custom comment app API<a class="headerlink" href="#custom-comment-app-api" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="index.html#module-django.contrib.comments" title="django.contrib.comments: Django's comment framework"><tt class="xref py py-mod docutils literal"><span class="pre">django.contrib.comments</span></tt></a> app defines the following methods; any
custom comment app must define at least one of them. All are optional,
however.</p>
<dl class="function">
<dt id="django.contrib.comments.get_model">
<tt class="descname">get_model</tt>()<a class="reference internal" href="../../../_modules/django/contrib/comments.html#get_model"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.comments.get_model" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the <a class="reference internal" href="../../models/instances.html#django.db.models.Model" title="django.db.models.Model"><tt class="xref py py-class docutils literal"><span class="pre">Model</span></tt></a> class to use for comments. This
model should inherit from
<tt class="docutils literal"><span class="pre">django.contrib.comments.models.BaseCommentAbstractModel</span></tt>, which
defines necessary core fields.</p>
<p>The default implementation returns
<a class="reference internal" href="models.html#django.contrib.comments.models.Comment" title="django.contrib.comments.models.Comment"><tt class="xref py py-class docutils literal"><span class="pre">django.contrib.comments.models.Comment</span></tt></a>.</p>
</dd></dl>

<dl class="function">
<dt id="django.contrib.comments.get_form">
<tt class="descname">get_form</tt>()<a class="reference internal" href="../../../_modules/django/contrib/comments.html#get_form"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.comments.get_form" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the <a class="reference internal" href="../../forms/api.html#django.forms.Form" title="django.forms.Form"><tt class="xref py py-class docutils literal"><span class="pre">Form</span></tt></a> class you want to use for
creating, validating, and saving your comment model.  Your custom
comment form should accept an additional first argument,
<tt class="docutils literal"><span class="pre">target_object</span></tt>, which is the object the comment will be
attached to.</p>
<p>The default implementation returns
<a class="reference internal" href="forms.html#django.contrib.comments.forms.CommentForm" title="django.contrib.comments.forms.CommentForm"><tt class="xref py py-class docutils literal"><span class="pre">django.contrib.comments.forms.CommentForm</span></tt></a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The default comment form also includes a number of unobtrusive
spam-prevention features (see
<a class="reference internal" href="index.html#notes-on-the-comment-form"><em>Notes on the comment form</em></a>).  If replacing it with your
own form, you may want to look at the source code for the
built-in form and consider incorporating similar features.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="django.contrib.comments.get_form_target">
<tt class="descname">get_form_target</tt>()<a class="reference internal" href="../../../_modules/django/contrib/comments.html#get_form_target"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.comments.get_form_target" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the URL for POSTing comments. This will be the <tt class="docutils literal"><span class="pre">&lt;form</span> <span class="pre">action&gt;</span></tt>
attribute when rendering your comment form.</p>
<p>The default implementation returns a reverse-resolved URL pointing
to the <tt class="docutils literal"><span class="pre">post_comment()</span></tt> view.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you provide a custom comment model and/or form, but you
want to use the default <tt class="docutils literal"><span class="pre">post_comment()</span></tt> view, you will
need to be aware that it requires the model and form to have
certain additional attributes and methods: see the
<tt class="docutils literal"><span class="pre">django.contrib.comments.views.post_comment()</span></tt> view for details.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="django.contrib.comments.get_flag_url">
<tt class="descname">get_flag_url</tt>()<a class="reference internal" href="../../../_modules/django/contrib/comments.html#get_flag_url"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.comments.get_flag_url" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the URL for the &#8220;flag this comment&#8221; view.</p>
<p>The default implementation returns a reverse-resolved URL pointing
to the <tt class="docutils literal"><span class="pre">django.contrib.comments.views.moderation.flag()</span></tt> view.</p>
</dd></dl>

<dl class="function">
<dt id="django.contrib.comments.get_delete_url">
<tt class="descname">get_delete_url</tt>()<a class="reference internal" href="../../../_modules/django/contrib/comments.html#get_delete_url"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.comments.get_delete_url" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the URL for the &#8220;delete this comment&#8221; view.</p>
<p>The default implementation returns a reverse-resolved URL pointing
to the <tt class="docutils literal"><span class="pre">django.contrib.comments.views.moderation.delete()</span></tt> view.</p>
</dd></dl>

<dl class="function">
<dt id="django.contrib.comments.get_approve_url">
<tt class="descname">get_approve_url</tt>()<a class="reference internal" href="../../../_modules/django/contrib/comments.html#get_approve_url"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.comments.get_approve_url" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the URL for the &#8220;approve this comment from moderation&#8221; view.</p>
<p>The default implementation returns a reverse-resolved URL pointing
to the <tt class="docutils literal"><span class="pre">django.contrib.comments.views.moderation.approve()</span></tt> view.</p>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Customizing the comments framework</a><ul>
<li><a class="reference internal" href="#an-example-custom-comments-app">An example custom comments app</a></li>
<li><a class="reference internal" href="#custom-comment-app-api">Custom comment app API</a></li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="signals.html">Signals sent by the comments app</a></li>
    
    
      <li>Next: <a href="forms.html">Comment form classes</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../index.html">API Reference</a>
        
          <ul><li><a href="../index.html"><tt class="docutils literal"><span class="pre">contrib</span></tt> packages</a>
        
          <ul><li><a href="index.html">Django&#8217;s comments framework</a>
        
        <ul><li>Customizing the comments framework</li></ul>
        </li></ul></li></ul></li></ul>
      </li>
  </ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../../_sources/ref/contrib/comments/custom.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="signals.html" title="Signals sent by the comments app">previous</a>
     |
    <a href="../../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="forms.html" title="Comment form classes">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>